
function ShowRoutedialog()
{
	if($("input[name*=checkbox_payment_]").is(":checked") != true)
	{
		simpleDialog("select the Payment !!",100,null);
	}else
	{
		var buttons = [
				{ title:btnrutechargeOk , callback: function(thisdialog,args){
					RoutePayment();
			thisdialog.dialog("close");
				}},
				{ title:btnrutechargeCancel , callback: function(thisdialog,args){
					thisdialog.dialog("close");
					}}
				];
				customDialog(routePayment,$("#route_payment").html(),{ width: 406},buttons,null);
				$("[id*=route-payment-to]").children().removeAttr("style");
				$("[id*=route-payment-to]").children().removeAttr("selected");
		var checkedPayment =[];
		$("input[name*=checkbox_payment_]:visible:checked").each(function(){
			checkedPayment.push($(this).attr("value"));
			
				});
		
		var link = "/paymentService?func=calPayment&paymentId=" + checkedPayment;
		simpleserviceWithArgs(link,null,ShowRoutedialogSuccess,null);
	}
}

function ShowRoutedialogSuccess(data,obj)
{
	var objArray = new Array();
	objArray = JSON.parse(data);
	if(objArray  == null){
		simpleDialog(saveFail_label,100,null);
	}
	else
	{
		$("[id*=route-payment-total]:visible").text(parseFloat(data).toFixed(currencyDecimalPlaces));
	}
}

function GetOwnerofPayment(id)
{
	$("[id*=route-payment-to]").children().removeAttr("style");
	$("[id*=route-payment-to]").children().removeAttr("selected");
	var folio=$(id).attr("folioId");
	var balance=parseFloat($(id).attr("balance"));
	if(balance<0)
	{
		$("[id*=route-payment-amount]:visible").val(parseFloat(0).toFixed(currencyDecimalPlaces));
	}else
	{
		$("[id*=route-payment-amount]:visible").val(balance.toFixed(currencyDecimalPlaces));
	}
	$("[folioId="+folio+"]").css("background-color","#cccccf");
	$("[folioId="+folio+"]").attr("selected",true);
}

function RoutePayment()
{
	var arrayFolio="";
	var amount = $("[id*=route-payment-amount]:visible").val();
	var balance =$("[id*=route-payment-total]:visible").text();
	if($.isNumeric(amount) && parseFloat(balance)!=0)
	{
		$("[id*=route-payment-amount]:visible").val(amount);
			if(arrayFolio != null)
			{
			var folio = $("[id*=route-payment-to]:visible [selected=selected]").attr("folioId");
			var checkedPayment =[];
			$("input[name*=checkbox_payment_]:visible:checked").each(function(){
				checkedPayment.push($(this).attr("value"));
			});
			var link = "/subPaymentService?func=addRoutePayment&folioId="+folio+"&amount="+amount+"&PaymentList="+checkedPayment;
			simpleserviceWithArgs(link,null,RoutePaymentSuccess,null);
			}else
			{
			simpleDialog("select folio .",100,null);
			}
		}else
		{
		simpleDialog("input moneyagain.",100,null);
		$("[id*=route-payment-amount]:visible").val(null);	
		}
}

function RoutePaymentSuccess(data,obj)
{
	var objArray = new Array();
	objArray = JSON.parse(data);
	if(objArray  == null){
		simpleDialog(saveFail_label,100,null);
	}else
	{
		var checkLogOut =  CheckSession(objArray);
		if(checkLogOut)
			return;
		var Tabs = getCurrentTabData();
		var url="/folioService?func=loadSingleFolioList&resId="+Tabs.resId;
		simpleserviceWithArgs(url,null,loadFolioSuccess,null);
	}
}

function SetFolio()
{
	var folioList =[];
	var listPayment=[];
	$("input[name*=payment-folio-chekbox]:visible").each(function(){
		folioList.push($(this).attr("folioid"));
			});
	$("input[name*=checkbox_payment_]:visible").each(function(){
		listPayment.push($(this).attr("value"));
		
			});
		var link = "/paymentService?func=setFolio&listFolio="+folioList+"&listPayment="+listPayment;
		simpleserviceWithArgs(link,null,SetFolioSuccess,null);
	}

function SetFolioSuccess(data,obj)
{
	objArray = JSON.parse(data);
	var checkLogOut = CheckSession(objArray);
	if(checkLogOut)
		return;
	if(data  == 0){
		simpleDialog(saveFail_label,100,null);
	}else
	{
		fillFolio();
	}
}

function CheckAmount()
{
	var amount = $("[id*=route-payment-amount]:visible").val();	
	var balance =$("[id*=route-payment-total]:visible").text();
	if($.isNumeric(amount) && parseFloat(balance)!=0)
	{
		$("[id*=route-payment-amount]:visible").val(amount);
	}else
	{
		simpleDialog("input moneyagain.",100,null);
		$("[id*=route-payment-amount]:visible").val(null);
	}
}
//--route to folio--//
function ShowRouteChargedialog()
{ 
	if($("input[name*=checkbox_charge_]").is(":checked") != true)
	{
		simpleDialog("select the charge !!",100,null);
	}else{
		var buttons = [
					{ title:btnrutechargeOk , callback: function(thisdialog,args){
						RouteNewFolio();
				thisdialog.dialog("close");
						//callCancelReaservation();
					}},
					{ title:btnrutechargeCancel , callback: function(thisdialog,args){
						thisdialog.dialog("close");
					}}
				];
				customDialog(ruteCharge,$("#route_charge_to_folio").html(),{ width: 406},buttons,null);
				var cost=$("input[name*=checkbox_charge_]:visible:checked").attr("cost");
				$("[name*=total_folio_cost]:visible").text(cost);
				$("[id*=route-to-folio-item]").children().removeAttr("style");
				$("[id*=route-to-folio-item]").children().removeAttr("selected");
	}
}
//--route new folio--//
function ShowRoutenewFoliodialog()
{
	if($("input[name*=checkbox_charge_]").is(":checked") != true)
	{
		simpleDialog("select the charge !!",100,null);
	}else{
		var buttons = [
					{ title:btnrutechargeOk , callback: function(thisdialog,args){
						RouteNewFolio();
				thisdialog.dialog("close");
					}},
					{ title:btnrutechargeCancel , callback: function(thisdialog,args){
						thisdialog.dialog("close");
					}}
				];
				customDialog(ruteCharge,$("#route_new_folio").html(),{ width: 406},buttons,null);
	}
}

function GetFolioOfcharge(id)
{
		$("[id*=route-to-folio-item]").children().removeAttr("style");
		$("[id*=route-to-folio-item]").children().removeAttr("selected");
		var folio=$(id).attr("folioId");
		$("[folioId="+folio+"]").css("background-color","#cccccf");
		$("[folioId="+folio+"]").attr("selected",true);
}

function RouteNewFolio()
{
//<--charge of reservation --//>
	var chargeArray  = new Array();
	var cArray  = new Array();
	var chargeResArray= new Array();
	var cResArray= [];
	var chargeGroupArray= new Array();
	$("input[name*=checkbox_charge_res]:visible:checked").each(function(){
		chargeResArray.push($(this).attr("resId"));
		});
	$.each(chargeResArray, function(i,el){
		if($.inArray(el, cResArray) === -1) cResArray.push(el);
	});
	for(var t = 0;t<cResArray.length;t++)
	{
		$("input[name*=checkbox_charge_res][resid="+cResArray[t]+"]:visible:checked").each(function(){
			chargeArray.push( $(this).attr("value"));
			});
		if(chargeArray.length == 1)
		{
			cArray.push(chargeArray);
			chargeArray=[];
		}
		else
		{
		var varText =chargeArray.toString().split(",");
		var charText="";
		for(var a=0;a<varText.length;a++)
		{
			if(a == (varText.length-1))
			{
				charText+=varText[a];
			}else
			{
				charText+=varText[a]+"_";
			}
		}
		cArray.push(charText);
		chargeArray=[];
		}
	}
	$("input[name*=checkbox_charge_group]:visible:checked").each(function() {
			chargeGroupArray.push($(this).attr("value"));
		});
	var chargeGroupId = $("[name*=checkbox_charge_group]:visible:checked").first().attr("resid");
	if(chargeGroupId == null)
	{
		chargeGroupId ="0";
	}
	//<--charge IN FOLIO--//>
	var chargeArray= new Array();
		$("input[name*=checkbox_charge]:visible:checked").each(function() {
			chargeArray.push( $(this).attr("value"));
			
		});
	var GroupId = $("[id*=route-new-folio-item]:visible option[name=groupId]:selected").val();
	if(GroupId == null)
	{
		GroupId ="0";
	}
	var resId = $("[id*=route-new-folio-item]:visible option[name=resId]:selected").val();
	if(resId == null)
	{
		resId ="0";
	}
	var folioId = $("[id*=route-to-folio-item]:visible [selected=selected]").attr("folioId");
	if(folioId == null)
	{
		folioId ="0";
	}
	var url = "/folioService?func=routenewFolio&chargeList="+chargeArray+"&gid="+GroupId+"&resId="+resId
	+"&resEditchargeId="+cResArray+"&Editchargegid="+chargeGroupId+"&folioId="+folioId;
	simpleserviceWithArgs(url,null,RouteNewFolioSuccess,null);
}

function RouteNewFolioSuccess(data,obj)
{
	var objArray = new Array();
	objArray = JSON.parse(data);
	if(objArray  == null){
		simpleDialog(saveFail_label,100,null);
	}
	else
	{
		var checkLogOut =  CheckSession(objArray);
		if(checkLogOut)
			return;
		var Tabs = getCurrentTabData();
		if(Tabs.type == "Single")
		{
			Tabs.FolioList=objArray;
			var url="/folioService?func=loadSingleFolioList&resId="+Tabs.resId;
			simpleserviceWithArgs(url,null,loadFolioSuccess,null);
		}else
		{
			ShowFolio();
		}
	}
}